/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.entity.VCard;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.ShareEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.mapper.FxShareBrowseMapper;
import com.ykht.proj.coupon.service.FxShareBrowseService;
import com.ykht.proj.coupon.vo.BaseSearchTimeVo;
import com.ykht.proj.coupon.vo.ShareBrowse4C;
import com.ykht.proj.coupon.vo.ShareBrowse4CStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分享浏览表
 *
 * @author proj code generator
 * @date 2021-08-12 14:10:39
 */
@Service
@SuppressWarnings("all")
public class FxShareBrowseServiceImpl extends ServiceImpl<FxShareBrowseMapper, FxShareBrowse> implements FxShareBrowseService {


	@Autowired
	private FxShareBrowseMapper fxShareBrowseMapper;

	@Override
	public ShareBrowse4CStatistics findPage(Page<ShareBrowse4C> page, BaseSearchTimeVo searchTimeVo) {
		optTimeVo(searchTimeVo);
		ShareBrowse4CStatistics c = new ShareBrowse4CStatistics();
		ShareBrowse4C browse4C = new ShareBrowse4C();
		page = fxShareBrowseMapper.findPage(page,searchTimeVo);
		Set<Long> brandIds = new HashSet<>();
		Set<Long> shopIds = new HashSet<>();
		Set<Long> cmsIds = new HashSet<>();
		Set<Long> posterIds = new HashSet<>();
		Set<Long> cardIds = new HashSet<>();
		Set<Long> shareIds = new HashSet<>();

		page.getRecords().stream().forEach(o->{
			if (o.getTatId()!=null) {
				switch (ShareEnum.getThisByVal(o.getType())){
					case brand:
						brandIds.add(o.getTatId());
						break;
					case shop:
						shopIds.add(o.getTatId());
						break;
					case poster:
						posterIds.add(o.getTatId());
						break;
					case cms:
						cmsIds.add(o.getTatId());
						break;
					case card:
						cardIds.add(o.getTatId());
						break;
				}
				shareIds.add(o.getShareId());
			}
		});
		Map<Long, String> brandMap = new HashMap<>();
		Map<Long, String> shopMap = new HashMap<>();
		Map<Long, String> postMap = new HashMap<>();
		Map<Long, String> cmsMap = new HashMap<>();
		Map<Long, String> cardMap = new HashMap<>();
		Map<Long, Long> shareMap = new HashMap<>();
		if (brandIds.size() != 0) brandMap = new CnBrand().selectList(Wrappers.lambdaQuery(CnBrand.class).select(CnBrand::getId,CnBrand::getBrandName).in(CnBrand::getId, brandIds)).stream().collect(Collectors.toMap(CnBrand::getId, CnBrand::getBrandName));
		if (shopIds.size() != 0) shopMap = new CnShop().selectList(Wrappers.lambdaQuery(CnShop.class).select(CnShop::getId, CnShop::getName).in(CnShop::getId, shopIds)).stream().collect(Collectors.toMap(CnShop::getId, CnShop::getName));
		if (posterIds.size() != 0) postMap = new FxConfPosterBrand().selectList(Wrappers.lambdaQuery(FxConfPosterBrand.class).select(FxConfPosterBrand::getBrandId, FxConfPosterBrand::getName).in(FxConfPosterBrand::getBrandId, posterIds)).stream().collect(Collectors.toMap(FxConfPosterBrand::getBrandId, FxConfPosterBrand::getName));
		if (cmsIds.size() != 0) cmsMap = new FxCmsInfo().selectList(Wrappers.lambdaQuery(FxCmsInfo.class).select(FxCmsInfo::getId, FxCmsInfo::getPcTitle).in(FxCmsInfo::getId, cmsIds)).stream().collect(Collectors.toMap(FxCmsInfo::getId, FxCmsInfo::getPcTitle));
		if (cardIds.size() != 0) cardMap = new VCard().selectList(Wrappers.lambdaQuery(VCard.class).select(VCard::getId, VCard::getName).in(VCard::getId, cardIds)).stream().collect(Collectors.toMap(VCard::getId, VCard::getName));
		if (shareIds.size() != 0) shareMap = new FxRewardPerson().selectList(Wrappers.lambdaQuery(FxRewardPerson.class).select(FxRewardPerson::getId).in(FxRewardPerson::getShareId, shareIds)).stream().collect(Collectors.groupingBy(FxRewardPerson::getShareId, Collectors.counting()));
		Map<Long, String> finalBrandMap = brandMap;
		Map<Long, String> finalShopMap = shopMap;
		Map<Long, String> finalPostMap = postMap;
		Map<Long, String> finalCmsMap = cmsMap;
		Map<Long, String> finalCardMap = cardMap;
		Map<Long, Long> finalShareMap = shareMap;
		page.getRecords().stream().forEach(o->{
			if (o.getTatId()!=null) {
				switch (ShareEnum.getThisByVal(o.getType())) {
					case brand:
						o.setName(finalBrandMap.get(o.getTatId()));
						break;
					case shop:
						o.setName(finalShopMap.get(o.getTatId()));
						break;
					case poster:
						o.setName(finalPostMap.get(o.getTatId()));
						break;
					case cms:
						o.setName(finalCmsMap.get(o.getTatId()));
						break;
					case card:
						o.setName(finalCardMap.get(o.getTatId()));
						break;
				}
				o.setReciveNum(finalShareMap.get(o.getShareId()));
			}
		});
		Integer recivieNum = fxShareBrowseMapper.sumReciveNum(searchTimeVo);
		Integer browserNum = this.count(Wrappers.lambdaQuery(FxShareBrowse.class).eq(FxShareBrowse::getPerId, SecurityUtils.getPersonId()).between(BeanUtil.isNotEmpty(searchTimeVo),FxShareBrowse::getCreateTime,searchTimeVo.getStartTime(),searchTimeVo.getEndTime()));

		c.setBrowseNum((int)page.getTotal());
		c.setBrowserNum(browserNum);
		c.setReciveNum(recivieNum);
		c.setPage(page);
		return c;
	}

	public BaseSearchTimeVo optTimeVo(BaseSearchTimeVo timeVo) {
		if (timeVo.getOffsetDay() == null) return timeVo;
		if (timeVo.getOffsetDay()>0) timeVo.setOffsetDay(0);
		switch (timeVo.getOffsetDay()){
			case -1:
				timeVo.setStartTime(LocalDateTime.of(LocalDate.now().plusDays(timeVo.getOffsetDay()), LocalTime.MIN));
				timeVo.setEndTime(LocalDateTime.of(LocalDate.now().plusDays(timeVo.getOffsetDay()), LocalTime.MAX));
				return timeVo;
			default:
				timeVo.setStartTime(LocalDateTime.of(LocalDate.now().plusDays(timeVo.getOffsetDay()), LocalTime.MIN));
				timeVo.setEndTime(LocalDateTime.now());
				return timeVo;
		}
	}
}
